//
//  SZLCardPayWebViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/9/10.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLCardPayWebViewController.h"
#import <cmbkeyboard/CMBWebKeyboard.h>
#import <cmbkeyboard/NSString+Additions.h>
#import "PayTool.h"

@interface SZLCardPayWebViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic,weak) UIWebView *web;
@property (nonatomic,strong) NSURLRequest *request;

@end

@implementation SZLCardPayWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"银行卡支付";
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
    [backBtn setBackgroundImage:GetImage(@"btn_back_normal") forState:UIControlStateNormal];
    [backBtn setBackgroundImage:GetImage(@"btn_back_pressed") forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
    web.delegate = self;
    [self.view addSubview:web];
    self.web = web;
}
- (void)backBtnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    [self.web loadRequest:self.request];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[CMBWebKeyboard shareInstance] hideKeyboard];
}
static BOOL FROM = FALSE;
- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.host isCaseInsensitiveEqualToString:@"cmbls"]) {
        CMBWebKeyboard *secKeyboard = [CMBWebKeyboard shareInstance];
        [secKeyboard showKeyboardWithRequest:request];
        secKeyboard.webView = _webView;
        
        UITapGestureRecognizer* myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.view addGestureRecognizer:myTap]; //这个可以加到任何控件上,比如你只想响应WebView，我正好填满整个屏幕
        myTap.delegate = self;
        myTap.cancelsTouchesInView = NO;
        return NO;
    }else if ([request.URL.absoluteString isEqualToString:@"http://122.114.162.140/zs/pay/success"]){
        [[PayTool shareTool] checkPaySuccess];
        [self backBtnClick];
        return NO;
    }
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [[CMBWebKeyboard shareInstance] hideKeyboard];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Load webView error:%@", [error localizedDescription]);
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    if (FROM) {
        return;
    }
}

- (NSURLRequest *)request
{
    if (!_request) {
        _request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.webStr]];
    }
    return _request;
}
#pragma mark - dealloc
- (void)dealloc
{
    [[CMBWebKeyboard shareInstance] hideKeyboard];
}

@end
